package com.tenglong.teacher.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.glide.GlideOptions;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.StudentSignBottomInfo;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.homepage.SignItemViewGroup;
import com.thinkcool.circletextimageview.CircleTextImageView;

import java.util.List;

public class StudentSignAdapter extends BaseAdapter implements View.OnClickListener {
    private List<StudentSignBottomInfo> infos;
    private Context context;
    private StudentListener listener;

    public StudentSignAdapter(List<StudentSignBottomInfo> infos, Context context){
        this.infos = infos;
        this.context = context;
    }
    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.student_sign_item_layout,parent,false);
            holder = new ViewHolder();
            holder.classroomCase = convertView.findViewById(R.id.classroomCase);
            holder.image = convertView.findViewById(R.id.image);
            holder.name = convertView.findViewById(R.id.name);
            holder.sign_btn = convertView.findViewById(R.id.sign_btn);
            holder.iv_sele = convertView.findViewById(R.id.iv_sele);
            holder.rootView = convertView.findViewById(R.id.rootView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final StudentSignBottomInfo info = infos.get(position);
        RequestOptions options = GlideOptions.getGlideRequestOptions().
                error(R.drawable.setting_image_defauft_header).
                placeholder(R.drawable.header_image_loading);
        Glide.with(context)
                .load(IpUtils.getImageUrl(info.photoUrl))
                .apply(options)
                .into(holder.image);
        holder.iv_sele.setVisibility(info.isShowCircleQuan?View.VISIBLE:View.GONE);
        holder.rootView.setChildIsClick(info.isShowCircleQuan);
        holder.iv_sele.setSelected(info.isSele);
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (info.isShowCircleQuan){
                    holder.iv_sele.setSelected(info.isSele = !info.isSele);
                }
            }
        });
        holder.sign_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener==null)return;
                listener.onSign(new StudentListener.Result() {
                    @Override
                    public void onSucceed() {

                    }

                    @Override
                    public void onFailed() {

                    }
                });
            }
        });
        holder.classroomCase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener==null)return;
                listener.onClassroom();
            }
        });

        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sign_btn:
                break;
            case R.id.classroomCase:
                break;
        }
    }

    public void setOnItemStudentListener(StudentListener listener){
        this.listener = listener;
    }


    class ViewHolder{
        CircleTextImageView image;
        TextView name;
        TextView sign_btn;
        TextView classroomCase;
        ImageView iv_sele;
        SignItemViewGroup rootView;
    }

    public interface StudentListener{
        void onSign(Result result);

        void onClassroom();

        void onSlide();

        interface Result{
            void onSucceed();
            void onFailed();
        }
    }
}
